package com.example.administrator.myapplication;

import android.graphics.Color;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    SwipeRefreshLayout mSR;

    ArrayList<String> dataList;// 数据

    RecyclerView Rv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initData();

        initView();

        //  找到控件对象
        mSR =findViewById(R.id.main);


        //  设置刷新状态变换事件处理方法
        mSR.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

                // 获取数据，更新界面
                 mSR.setBackgroundColor(Color.BLUE);
                 //  模拟耗时操作  线程
                 new Thread(new Runnable() {
                     @Override
                     public void run() {
                         try {
                             // 休眠5秒
                             Thread.sleep(5000);
                             //  调用UI线程状态提示 在UI线程中执行
                             runOnUiThread(new Runnable() {
                                 @Override
                                 public void run() {
                                     // 关闭刷新状态提示
                                     mSR.setRefreshing(false);
                                 }
                             });
                         } catch (InterruptedException e) {
                             e.printStackTrace();
                         }
                     }
                 }).start();


            }
        });

    }

    private void initView() {
        MyAdapter adapter = new MyAdapter();
        Rv = findViewById(R.id.rlv);
//        Rv.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,true));

        Rv.setLayoutManager(new GridLayoutManager(this,3));//  布局几例成

        Rv.setAdapter(adapter);
    }

    private void initData() {
        dataList = new ArrayList<>();
        for (int i = 0; i <100; i++) {
            dataList.add("item"+i);
        }
    }

    class MyAdapter extends RecyclerView.Adapter{

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            MyViewHolder myViewHolder = new MyViewHolder(
                    View.inflate(MainActivity.this,
                    R.layout.list_item_layout,null));
            return myViewHolder;
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            ((MyViewHolder)holder).tv.setText(dataList.get(position));
        }

        @Override
        public int getItemCount() {
            return dataList.size();
        }

        // 定义
        class MyViewHolder extends RecyclerView.ViewHolder{
             TextView tv;
            public MyViewHolder(View itemView) {
                super(itemView);
                tv = itemView.findViewById(R.id.tv);
            }
        }
    }
}
